﻿using AutoMapper;
using MediatR;
using SmartTracking.API.Write.Application.Commands.SheepFarm_command;
using SmartTracking.Domain.Sheep;
using SmartTracking.ErrorCode;
using SmartTracking.Infrastructure.Interfaces;
using Yitter.IdGenerator;

namespace SmartTracking.API.Write.Application.CommandHandlers.SheepFarm_handler
{
    public class AddSheepFarmCommandHandler:IRequestHandler<AddSheepFarmCommand,ApiResult<int>>
    {
        private readonly IBaseRepository<SheepFarm> sheepFarmRep;
        private readonly IMapper mapper;

        public AddSheepFarmCommandHandler(
            IBaseRepository<SheepFarm> sheepFarmRep,
            IMapper mapper)
            {
            this.sheepFarmRep = sheepFarmRep;
            this.mapper = mapper;
        }

        public async Task<ApiResult<int>> Handle(AddSheepFarmCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.新增成功;
            result.Message = ApiEnum.新增成功.ToString();

            SheepFarm sheep = mapper.Map<SheepFarm>(request);
            sheep.IsDeleted = false;
            sheep.FarmNo = YitIdHelper.NextId().ToString();
            sheep.CreateTime = DateTime.Now;
            var list = await sheepFarmRep.AddAsync(sheep);

            result.data = list;
            return await Task.FromResult(result);
        }
    }
}
